package pl.edu.agh.portal.application.portlets;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletConfig;
import javax.portlet.PortletException;
import javax.portlet.PortletMode;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import pl.edu.agh.portal.application.utils.PortletsUtils;
import pl.edu.agh.thesis.webservices.employee.EmployeeManagerSkeleton;
import pl.edu.agh.thesis.webservices.employee.client.EmployeeManagerStub;
import pl.edu.agh.thesis.webservices.employee.client.EmployeeUtils;
import pl.edu.agh.thesis.webservices.project.client.ProjectManagerStub;
import pl.edu.agh.thesis.webservices.project.client.ProjectUtils;

public class ProjectEditPortlet extends ThesisGenericPortlet {
	private static final String NORMAL_VIEW = "/project_edit_portlet.jsp";
	
	private PortletRequestDispatcher normalView;
	
	private Map<String, String> errorList = new HashMap<String, String>();
	
	private ProjectManagerStub.Project project;	
		
	public void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException {
		String projectIdString = (String)PortletsUtils.vault.get("projectId");
		int projectId = Integer.parseInt(projectIdString);
				
		project = ProjectUtils.get(PortletsUtils.temp_session, projectId);
		
		//TODO: UZUPELNIC O SPRAWDZANIE ROLI, TYLKO ADMIN MOZE EDYTOWAC PROJEKT
		
		request.setAttribute(PortletsUtils.ATTRIBUTE_PROJECT_FOR_EDITION, project);
		request.setAttribute(PortletsUtils.ATTRIBUTE_ERROR_MESSAGES, errorList);
						
		normalView.include(request, response);
	}
	
	@Override
	public void processAction(ActionRequest request, ActionResponse response) throws PortletException, IOException {
		
		String value = request.getParameter(PortletsUtils.PROJECT_EDITION_KEY);		
		String address = "/portal/portal/project_details.psml";
		
		if (value.equals(PortletsUtils.BUTTON_CANCEL)) {			
			response.sendRedirect(address);
		} else if (value.equals(PortletsUtils.BUTTON_SAVE)) {
			validate(request);
			if (errorList.size() == 0) {
				
				int thisEmployeeId = EmployeeUtils.validate(PortletsUtils.temp_session);
				EmployeeManagerStub.Employee[] oldProjectEmployees = EmployeeUtils.getAll(PortletsUtils.temp_session, EmployeeManagerSkeleton.GET_EMPLOYEE_IN_PROJECT, "" + project.getId());
				String[] newProjectEmployees = request.getParameterValues(PortletsUtils.PARAMETER_PROJECT_USERS);
				
				if (oldProjectEmployees == null) oldProjectEmployees = new EmployeeManagerStub.Employee[0];
				if (newProjectEmployees == null) newProjectEmployees = new String[0];
				
				List<Integer> toAdd = new ArrayList<Integer>();
				List<Integer> toRemove = new ArrayList<Integer>();
				for (EmployeeManagerStub.Employee oldEmployee : oldProjectEmployees) {
					if (oldEmployee.getId() != thisEmployeeId) {
						boolean flag = false;
						for (String newEmployeeId : newProjectEmployees) {
							if (Integer.parseInt(newEmployeeId) == oldEmployee.getId()) {
								flag = true;
							}
						}
						if (!flag) {
							toRemove.add(oldEmployee.getId());
						}
					} 
				}
				
				for (String newEmployeeId : newProjectEmployees) {
					if (Integer.parseInt(newEmployeeId) != thisEmployeeId) {
						boolean flag = false;
						for (EmployeeManagerStub.Employee oldEmployee : oldProjectEmployees) {
							if (Integer.parseInt(newEmployeeId) == oldEmployee.getId()) {
								flag = true;
							}
						}
						if (!flag) {
							toAdd.add(Integer.parseInt(newEmployeeId));
						}
					} 
				}
				
				for (Integer employeeId : toRemove) {										
					EmployeeUtils.assign(PortletsUtils.temp_session, employeeId, project.getId(), PortletsUtils.ROLE_DELETE);				
				}
				
				for (Integer employeeId : toAdd) {										
					EmployeeUtils.assign(PortletsUtils.temp_session, employeeId, project.getId(), PortletsUtils.ROLE_USER);				
				}
				
				ProjectUtils.modify(project.getProjectName(), PortletsUtils.temp_session, project);
				response.sendRedirect(address);
			}			
		} else {
			response.setPortletMode(PortletMode.VIEW);
		}
	}	
	
	public void init(PortletConfig config) throws PortletException {
		super.init(config);
		normalView = config.getPortletContext().getRequestDispatcher(NORMAL_VIEW);
	}
	
	public void destroy() {
		normalView = null;
		super.destroy();
	}
 
	private void validate(ActionRequest request) {
			
		String parameterProjectName = request.getParameter(PortletsUtils.PARAMETER_PROJECT_NAME);
		String parameterProjectDescription = request.getParameter(PortletsUtils.PARAMETER_PROJECT_DESCRIPTION);
		
		errorList = new HashMap<String, String>(); 
		if (parameterProjectName != null && parameterProjectName.trim().equals("")) {
			errorList.put(PortletsUtils.ERROR_PARAMETER_PROJECT_NAME, PortletsUtils.ERROR_PARAMETER_PROJECT_NAME);
		} else {
			project.setProjectName(parameterProjectName);
		}
		if (parameterProjectDescription != null && parameterProjectDescription.trim().equals("")) {			
			errorList.put(PortletsUtils.ERROR_PARAMETER_PROJECT_DESCRIPTION, PortletsUtils.ERROR_PARAMETER_PROJECT_DESCRIPTION);
		} else {
			project.setDescription(parameterProjectDescription);
		}
		
	}
}
